package com.appology.chuck;

import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

public abstract class ChuckUserBuilder {

	public static ChuckUser getChuckUserByGoogleId(String googleId){
		DatastoreService datastore =  DatastoreServiceFactory.getDatastoreService();     
		 
	    Query query = new Query( "ChuckUser");
	    query.addFilter("googleId", Query.FilterOperator.EQUAL, googleId);
	    
	    List<Entity> users  = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1));  
	      if (users.isEmpty()) 
	      {
	    	  	return null;
	      }
	      else
	      {
	    	  ChuckUser chuckUser = new ChuckUser();
	    	  for (Entity user : users) 
	          {     
	    		  
	    		  chuckUser.setGoogleId(user.getProperty("googleId").toString());
	    		  chuckUser.setBuzzId(user.getProperty("buzzId").toString());
	    		  chuckUser.setDisplayName(user.getProperty("displayName").toString());
	          }
	    	  return chuckUser;
	     }
	      
	}
}
